package com.pzr.chatroom.controller;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.pzr.chatroom.entity.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import com.pzr.chatroom.entity.CacheBean;
import com.pzr.chatroom.entity.ChatContent;
import com.pzr.chatroom.entity.User;

import javax.annotation.Resource;
import javax.websocket.Session;


/**
 * 演示控制器
 * @author pzr
 *
 */
@Controller
public class ChatRoomController {
	
	 @Autowired
	 private SimpMessagingTemplate template;
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");




	/**
	 * 聊天
	 * @param content
	 * @return
	 */
	@MessageMapping("/talk")//浏览器映射地址, 接受消息
	public void talk(ChatContent content) {
		System.out.println("----------"+content.getContent());
		List<ChatContent> list = CacheBean.contentMap.get(content.getRoomid());
		if(list==null){
			list=new ArrayList<ChatContent>();
			CacheBean.contentMap.put(content.getRoomid(),list);
		}
		list.add(content);
		User user = new User();
		user.setRoomid(content.getRoomid());
		UserHolder.set(user);
		refreshchatwindow();

	}
	
	/**
	 * 下线
	 * @param msg
	 * @return
	 */
	@MessageMapping("/downLine")
	private void downLine(User msg) {
		List<User> list = CacheBean.clientList.get(msg.getRoomid());
		list.remove(msg);
		//服务器端通知客户端刷新当前登录人列表
		refreshLoginList();
	}

	/**
	 * 上线
	 * @param msg
	 * @return
	 */
	@MessageMapping("/upLine")
	@SendTo("/refreshchatwindow")
	private ChatContent upLine(User msg) {
		//内存中将用户加入进来
		List<User> list = CacheBean.clientList.get(msg.getRoomid());
		if(list==null){
			list=new ArrayList<User>();
			CacheBean.clientList.put(msg.getRoomid(),list);
		}
		list.add(msg);
		UserHolder.set(msg);
		//服务器端通知客户端刷新当前登录人列表
		refreshLoginList();
		final ChatContent content=new ChatContent();
		content.setDate(sdf.format(new Date()));
		content.setContent("["+msg.getName()+"]加入");
		return content;
	}


	
	/**
	 * 服务器端通知客户端刷新当前登录人列表
	 * @param list
	 */
	@MessageMapping("/refreshLoginList")
	public void refreshLoginList( ){
		User user = UserHolder.get();
		if(user==null){
			return;
		}
		if(user.getRoomid()!=null){
			List<User> users = CacheBean.clientList.get(user.getRoomid());
			if(users!=null){
				template.convertAndSend("/refreshloginlist",users);
			}
		}
	}


	/**
	 * 要通知的用户
	 */
	@SendTo("/refreshchatwindow")
	public void refreshchatwindow( ){
		User user = UserHolder.get();
		if(user==null){
			return;
		}
		if(user.getRoomid()!=null){
			List<User> users = CacheBean.clientList.get(user.getRoomid());
			if(users!=null){
				template.convertAndSend("/refreshchatwindow",users);
			}
		}
	}

	@SendTo("/refreshchatwindow")
	public ChatContent refreshchatwindowA(){
		for (String key:CacheBean.contentMap.keySet() ) {
			List<ChatContent> chatContents = CacheBean.contentMap.get(key);
			if(chatContents.size()>0){
				return CacheBean.contentMap.get(key).get(0);
			}
		}
		return new ChatContent();
	}

}
